package com.example.myapplication.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.myapplication.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class f2 extends Fragment {

    private EditText editAccount;
    private EditText editPassword;
    private EditText editPasswordAgain;
    private RadioButton rbBoy;
    private RadioButton rbGirl;
    private CheckBox box1;
    private CheckBox box2;
    private CheckBox box3;
    private CheckBox box4;
    private EditText editMore;
    private Button btRegister;
    View view;
    Context context;
    private RadioGroup radioGroup;

    public f2() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_f2, container, false);
        context = getContext();
        initView();
        Visible();
        checkpassword();
        return view;
    }

    /*
    初始化控件
     */
    private void initView() {
        editAccount = (EditText) view.findViewById(R.id.edit_account);
        editPassword = (EditText) view.findViewById(R.id.edit_password);
        editPasswordAgain = (EditText) view.findViewById(R.id.edit_password_again);
        radioGroup = (RadioGroup) view. findViewById(R.id.radioGroup);
        rbBoy = (RadioButton) view.findViewById(R.id.rb_boy);
        rbGirl = (RadioButton) view.findViewById(R.id.rb_girl);
        box1 = (CheckBox) view.findViewById(R.id.box1);
        box2 = (CheckBox) view.findViewById(R.id.box2);
        box3 = (CheckBox) view.findViewById(R.id.box3);
        box4 = (CheckBox) view.findViewById(R.id.box4);
        editMore = (EditText) view.findViewById(R.id.edit_more);
        btRegister = (Button) view.findViewById(R.id.bt_register);
        editMore.setVisibility(View.GONE);
    }
/*
其他按钮被选中显示出一个EditText
 */
void Visible(){
    box4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (box4.isChecked()) {
                editMore.setVisibility(View.VISIBLE);
            }else {
                editMore.setVisibility(View.GONE);
            }
        }
    });
}
    /*
    检查两次密码是否相同,且密码不能为空
     */
    public void checkpassword() {
        btRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (editPassword.getText().toString().equals("") || editPasswordAgain.getText().toString().equals("")) {
                    Toast.makeText(context, "密码不能为空", Toast.LENGTH_SHORT).show();
                }else {
                    if (!editPassword.getText().toString().equals(editPasswordAgain.getText().toString())) {
                        Toast.makeText(context, "两次输入密码不同", Toast.LENGTH_SHORT).show();
                        editPasswordAgain.setText("");
                    } else {
                        String account=getSting(editAccount);
                        String password=getSting(editPassword);
                        String more=getSting(editMore);
                        String b1=getSting(box1);
                        String b2=getSting(box2);
                        String b3=getSting(box3);
                        RadioButton radioButton = (RadioButton) view.findViewById(radioGroup.getCheckedRadioButtonId());
                        if (box1.isChecked()&&(!box2.isChecked())&&(!box3.isChecked())&&(!box4.isChecked())){
                            Toast.makeText(context, "用户名:"+account+"密码:"+password+"性别:"+radioButton.getText().toString()
                                    +",喜欢的明星" +b1, Toast.LENGTH_SHORT).show();
                        }
                        if (box1.isChecked()&&(box2.isChecked())&&(!box3.isChecked())&&(!box4.isChecked())) {
                            Toast.makeText(context, "用户名:"+account+"密码:"+password+"性别:"+radioButton.getText().toString()
                                    +",喜欢的明星" +b1+b2, Toast.LENGTH_SHORT).show();

                        }
                        if (box1.isChecked()&&(box2.isChecked())&&(box3.isChecked())&&(!box4.isChecked())) {
                            Toast.makeText(context, "用户名:"+account+"密码:"+password+"性别:"+radioButton.getText().toString()
                                    +",喜欢的明星" +b1+b2+b3, Toast.LENGTH_SHORT).show();
                        }
                        if (box1.isChecked()&&(box2.isChecked())&&(box3.isChecked())&&(box4.isChecked())) {
                        Toast.makeText(context, "用户名:"+account+"密码:"+password+"性别:"+radioButton.getText().toString()
                                +",喜欢的明星" +b1+b2+b3+more, Toast.LENGTH_SHORT).show();}

                    }
                }
            }
        });

    }
    String getSting(CheckBox edit) {
        return   edit.getText().toString()+",";
    }
/*
获取EditText字符串
 */

    String getSting(EditText edit) {
      return   edit.getText().toString()+",";
    }
}
